package cn.echoedu.specifics;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.AClass;
import cn.echoedu.entity.ClassNotify;
import cn.echoedu.entity.Department;
import cn.echoedu.entity.ReceiveClassNotify;
import cn.echoedu.entity.User;
import cn.echoedu.util.L;
import cn.echoedu.util.StringUtil;

public class SpecReceiveClassNotify extends BaseSpec{
    private final String T = "SpecReceiveClassNotify-------------->>";
	@Override
	public <T> PreparedStatement getPreparedInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> PreparedStatement getPreparedInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		L.gpir(T);
		ReceiveClassNotify receiveclassnotify = (ReceiveClassNotify)currentPojo;
		String sql = "insert into receiveclassnotify" +
				"(receiveusernumber,receiveclasscode," +
				"classnotifyisdefault,classnotifyid,receivedepartmentcode) " +
				"values" +
				StringUtil.getBeforeTheBsrackets()+"?,?,?,?,?"
				+StringUtil.getAfterTheBrackets();
		PreparedStatement preparedstatement = con.prepareStatement(sql);
		if(receiveclassnotify.getUser()!=null&&receiveclassnotify.getUser()
				.getUsernumber()!=null){
			preparedstatement.setString(1,receiveclassnotify.getUser().getUsernumber());
		}else{
			preparedstatement.setNull(1,Types.INTEGER);
		}
		if(receiveclassnotify.getReceiveclass()!=null&&
				receiveclassnotify.getReceiveclass().getClasscode()!=0){
			preparedstatement.setInt(2,receiveclassnotify.getReceiveclass()
					.getClasscode());
		}else{
			preparedstatement.setNull(2, Types.INTEGER);
		}
		preparedstatement.setInt(3,receiveclassnotify.getIsdefault());
		if(receiveclassnotify.getClassnotify()!=null&&receiveclassnotify
				.getClassnotify().getClassnotify()!=0){
			preparedstatement.setInt(4,receiveclassnotify.getClassnotify()
					.getClassnotify());
		}else{
			preparedstatement.setNull(4,Types.INTEGER);
		}
		if(receiveclassnotify.getDepartment()!=null&&receiveclassnotify.getDepartment().getDepartmentcode()!=0){
			preparedstatement.setInt(5,receiveclassnotify.getDepartment().getDepartmentcode());
		}else{
			preparedstatement.setNull(5,Types.INTEGER);
		}
        return preparedstatement;
	}

	@Override
	public Object getPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> PreparedStatement getPreparedUpdate(Connection con,
			TableName tableName, T oldPojo, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> String getPreparedInsertGetPrimaryKeySql(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getLeftJoinPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getPojoBySql(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		ReceiveClassNotify receiveclassnotify = new ReceiveClassNotify();
		receiveclassnotify.setReceiveclassnotifyid(rs.getInt(1));
		User user = new User();
		user.setUsernumber(rs.getString(2));
		receiveclassnotify.setUser(user);
		AClass aclass = new AClass();
		aclass.setClasscode(rs.getInt(3));
		receiveclassnotify.setReceiveclass(aclass);
		receiveclassnotify.setIsdefault(rs.getInt(4));
		ClassNotify classnotify = new ClassNotify();
		classnotify.setClassnotify(rs.getInt(5));
		receiveclassnotify.setClassnotify(classnotify);
		Department department = new Department();
		department.setDepartmentcode(rs.getInt(6));
		receiveclassnotify.setDepartment(department);
		return receiveclassnotify;
	}

	@Override
	public <T> PreparedStatement getPreparedListInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> PreparedStatement getPreparedListInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		List<ReceiveClassNotify> receivelist = (List<ReceiveClassNotify>)currentPojo;
		String sql = "insert into receiveclassnotify(receiveusernumber," +
				"receiveclasscode,classnotifyisdefault,classnotifyid,receivedepartmentcode)" +
				" values(?,?,?,?,?)";
		PreparedStatement preparedstatement = con.prepareStatement(sql,
				PreparedStatement.RETURN_GENERATED_KEYS);
		for(int i=0,len=receivelist.size();i<len;i++){
			L.p("receive spec"+i);
			if(receivelist.get(i)!=null&&receivelist.get(i).getUser()!=null&&
					receivelist.get(i).getUser().getUsernumber()!=null)
			{
				preparedstatement.setString(1,receivelist.get(i).getUser().getUsernumber());
			}else{
				preparedstatement.setNull(1,Types.INTEGER);
			}
			
			if(receivelist.get(i)!=null&&receivelist.get(i).getReceiveclass()!=null&&
					receivelist.get(i).getReceiveclass().getClasscode()!=0){
				preparedstatement.setInt(2,receivelist.get(i).getReceiveclass().getClasscode());
			}else{
				preparedstatement.setNull(2,Types.INTEGER);
			}
			
	        preparedstatement.setInt(3,receivelist.get(i).getIsdefault());
            
			if(receivelist.get(i)!=null&&receivelist.get(i).getClassnotify()!=null
					&&receivelist.get(i).getClassnotify().getClassnotify()!=0){
				preparedstatement.setInt(4, receivelist.get(i).getClassnotify().getClassnotify());
			}else{
				preparedstatement.setNull(4,Types.INTEGER);
			}
			if(receivelist.get(i)!=null&&receivelist.get(i).getDepartment()!=null
					&&receivelist.get(i).getDepartment().getDepartmentcode()!=0){
				preparedstatement.setInt(5,receivelist.get(i).getDepartment().getDepartmentcode());
			}else{
				preparedstatement.setNull(5,Types.INTEGER);
			}
			preparedstatement.addBatch();
		}
		return preparedstatement;
	}

}
